<template>
  <div class="list" ref="wapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-warpper" @click="handelCicks">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-warpper" v-for="itme of hotCity" :key="itme.id" @click="handelCick(itme.name)">
            <div class="button">{{itme.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(itme, key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="itme-list">
          <ul>
            <li class="itme border-bottom" v-for="inner of itme" :key="inner.id" @click="handelCick(inner.name)">{{inner.name}}</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/* better-scroll 是一款重点解决移动端（已支持 PC）各种滚动场景需求的插件 */
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hotCity: Array,
    cities: Object,
    lessCity: String
  },
  methods: {
    handelCick (city) {
      this.$store.commit('change', city)
      this.$router.push('/')
    },
    handelCicks () {
      this.$router.push('/')
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wapper)
  },
  watch: {
    lessCity () {
      if (this.lessCity) {
        const element = this.$refs[this.lessCity][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/config.styl'
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border-bottom
    &:before
      border-color: #ccc
  .list
    position: absolute
    top: 1.58rem
    bottom: 0
    left: 0
    right: 0
    overflow: hidden
  .title
    height: .6rem
    line-height: .6rem
    padding-left: .2rem
    background: #eee
    color: #666
    font-size: .26rem
  .button-list
    overflow: hidden
    padding: .1rem .6rem .1rem .1rem
    .button-warpper
      float: left
      width: 33.33%
      .button
        border: 1px solid #ccc
        margin: .1rem
        padding: .1rem 0
        border-radius: .06rem
        text-align: center
  .itme-list
    .itme
      line-height: .76rem
      padding-left: .2rem
</style>
